﻿//----------------------------------------------------------------
//CodeArt.
//
//文件描述:
//
//创 建 人: dreamnan@hotmail.com(Blog:http://www.cnblogs.com/dreamnan)
//创建日期: 2009-10-15
//
//修订记录: 
//
//----------------------------------------------------------------
using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Security;
using System.Threading;
using System.Xml;
using System.Web;
using System.Text;
using System.Collections.Generic;
using System.Collections.Specialized;


namespace CodeArt.SharePoint.Fields
{
    [CLSCompliant(false)]
    [SharePointPermissionAttribute(SecurityAction.Demand, ObjectModel = true)]
    public class SPSiteLookupField : SPFieldLookup
    {

        protected const string _allowPickerDialogProperty = "AllowPickerDialog";

        private bool _allowPickerDialog = false;

        public SPSiteLookupField(SPFieldCollection fields, string fieldName) : base(fields, fieldName)
        {
        }

        public SPSiteLookupField(SPFieldCollection fields, string typeName, string displayName)
            : base(fields, typeName, displayName)
        {

        }
        public override BaseFieldControl FieldRenderingControl
        {
            get
            {
                BaseFieldControl control = null;
                control = new LookupField();
                control.FieldName = base.InternalName;
                return control;
            }
        }

        public virtual bool AllowPickerDialog
        {
            get
            {
                object obj = base.GetCustomProperty(_allowPickerDialogProperty);
                //Boolean.TryParse(_linkToRelatedRecord, out value);
                return obj == null ? false : (bool)obj;
            }
            set
            {

                base.SetCustomProperty(_allowPickerDialogProperty, value);
                SetNamedThreadData(_allowPickerDialogProperty, value);
            }
        }


        #region Thread-local property persistence
        protected object GetCurrentPropertyValue(string propertyName)
        {
            object value = GetNamedThreadData(propertyName);
            if (value == null)
            {
                value = GetCustomProperty(propertyName);
            }
            return value;
        }

        protected object GetNamedThreadData(string propertyName)
        {
            return Thread.GetData(Thread.GetNamedDataSlot(propertyName));
        }

        protected void SetNamedThreadData(string propertyName, object value)
        {
            Thread.SetData(Thread.GetNamedDataSlot(propertyName), value);
        }

        protected void CleanNamedThreadData()
        {
            Thread.FreeNamedDataSlot(_allowPickerDialogProperty);
        }

        #endregion


        bool _preventUpdate = false;

        public override void OnAdded(SPAddFieldOptions op)
        {
            _preventUpdate = true;
            UpdateFieldProperties();
            //base.OnAdded(op);
            base.Update();
            CleanNamedThreadData();
        }

        public override void OnUpdated()
        {
            if (_preventUpdate == false)//避免递归触发Update事件
            {
                _preventUpdate = true;

                UpdateFieldProperties();

                //base.OnUpdated();
                base.Update();
                //Update();
                CleanNamedThreadData();
            }

        }

        void UpdateFieldProperties()
        {
            object allowPickerDialo = GetCurrentPropertyValue(_allowPickerDialogProperty);
            SetCustomProperty(_allowPickerDialogProperty, allowPickerDialo);
        }

    }
}
